var appValidate =
{
    _errBg	   :'#FF5555',
	_dflBg	   :'#CECECE',
	_errorMark:'background',
    _regExp    :{},
    _tmp_dt    :"Test", 
    _fromName  :".app_validation", 
    _error     :false,
    _errLog    :Array(),     
    _tipz	   :null,	
    check      :function(e)
                {
					$$('.inline-error').dispose();
					appValidate._error = false;
                    $$(this).getElements('input[type=password],input[type=text],textarea')[0].each(function(_obj,index){
						
                        if(_obj.offsetHeight > 0 || _obj.hasClass('richtexteditor'))
                        {
                            appValidate.reset();
                            if(_obj.hasClass("check_notempty")){appValidate.notEmpty(_obj);}
                            if(_obj.hasClass("check_email")){appValidate.email(_obj);}
                            if(_obj.hasClass("check_password")){appValidate.password(_obj);}
                            if(_obj.hasClass("check_isimage")){appValidate.isImage(_obj);}
                            if(_obj.hasClass("check_isdoc")){appValidate.isDoc(_obj);}
                            if(_obj.hasClass("check_ispdf")){appValidate.isPdf(_obj);}
                            if(_obj.hasClass("check_isnumber")){appValidate.isNumber(_obj);}
                            if(_obj.hasClass("check_alphanumeric")){appValidate.alphaNumeric(_obj);}
                            
                            if(appValidate._errLog.length>0)appValidate.displayErr(_obj,true);
                            else appValidate.displayErr(_obj,false);
                        }
                    });                    

					if(appValidate._error || _autoSubmit  == 'false')new Event(e).stop();

					if(typeof(apprain_a_s) != "undefined" && appValidate._error == false)
					{
						apprain_a_s.init();
						apprain_a_s.ajax_submit();
						
					}
                },
    reset      :function()
                {
                    appValidate._errLog = Array();
                },
    displayErr :function(_obj,errFlag)
                { 
                    if(errFlag)
					{
						appValidate._error = true;
						if(appValidate._errToolTips != 'false')appValidate.addTTips(_obj);

						if(appValidate._errorMark=='background')
						{
							_obj.setStyle('background-color',appValidate._errBg);
						}
						else if (appValidate._errorMark=='inline')
						{
							_obj.getParent().grab(new Element('div', {'class': 'inline-error'}).set('html',appValidate._errLog),'bottom');
						}
						else
						{
							_obj.setStyle('border','1px solid ' + appValidate._errBg);
						}
					}
                    else
					{
						if(appValidate._errToolTips != 'false' && appValidate._tipz != null)appValidate._tipz.detach(_obj);
						if(appValidate._errorMark=='background')_obj.setStyle('background-color', appValidate._dflBg);
						else _obj.setStyle('border','1px solid ' + appValidate._dflBg);
					}
                },
    addTTips   :function(_obj)
                {
                    _obj.store('tip:text', appValidate._errLog.join("<br />"));
                    appValidate._tipz = new Tips( _obj,{className: 'tipz'});						
                    appValidate._tipz.addEvents({
                        'show': function(tip) {tip.fade('in');},
                        'hide': function(tip) {tip.fade('out');}
                    });
                },
	addToErrLog :function(_obj,msg)
				{
					appValidate._errLog.push((_obj.get('longdesc') ? _obj.get('longdesc') : msg));
				},
     notEmpty   :function(_obj) 
                {
                    if(_obj.value=="")
					{
						appValidate.addToErrLog(_obj,'Please enter value here. This field can not be left empty');
					}
                },
    email      :function(_obj)
                {
                    if(!(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(_obj.value)))
                    {
						appValidate.addToErrLog(_obj,'Please enter a valid Email Address in ths field');
                    }
                },
	password   :function(_obj)
                {
					var re = /(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])/;
					
                    if(!re.test(_obj.value))
                    {
						appValidate.addToErrLog(_obj,'Password must be at least 8 characters and at least one lower case letter, one upper case letter and one digit');
                    }
                },
	isImage	   :function (_obj)
				{
					var ext = (/[.]/.exec(_obj.value)) ? /[^.]+$/.exec(_obj.value) : undefined;
					if(ext=="gif" || ext=="jpg" || ext=="png") return true;
					else
					{
						appValidate.addToErrLog(_obj,'Invalid image file entered. Supported extensions are *.gif, *.jpg, *.jpeg, *.png');
					}
				},
	isDoc	   :function (_obj)
				{
					var ext = (/[.]/.exec(_obj.value)) ? /[^.]+$/.exec(_obj.value) : "";
					if(ext=="doc" || ext=="docx" || ext=="txt" || ext=="rtf") return true;
					else
					{
						appValidate.addToErrLog(_obj,'Invalid document file entered. Supported extensions are *.doc, *.docx, *.txt, *.rtf');
					}
				},
	isPdf      :function (_obj)
				{
					var ext = (/[.]/.exec(_obj.value)) ? /[^.]+$/.exec(_obj.value) : undefined;
					if(ext=='pdf') return true;
					else
					{
						appValidate.addToErrLog(_obj,'Invalid Pdf file entered.');
					}
				},
	alphaNumeric:function (_obj)
				{
					var re = /^[\w ]+$/;
					if(!re.test(_obj.value))
                    {
						appValidate.addToErrLog(_obj,'Aplha numeric value required');
                    }
				},
	isNumber   :function (_obj)
				{
					if(isNaN(_obj.value) || _obj.value == '')
					{
						appValidate.addToErrLog(_obj,'Please enter a valid numeric vlaue.');
					}
				},
	init	   :function(e)
                {
				    try
				    {
						appValidate._errBg = _errBg;
						appValidate._dflBg = _dflBg;
						appValidate._errToolTips = _errToolTips;
						appValidate._errorMark = _errorMark;
						
				    }
				    catch (err){}
					$$(appValidate._fromName).addEvent('submit', appValidate.check);
                }
}

window.addEvent('domready', appValidate.init);